package com.example.notepad.timer

import android.os.Bundle
import android.os.CountDownTimer
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.notepad.R

class CountdownActivity : AppCompatActivity() {

    private lateinit var timerText: TextView
    private lateinit var btnStart: Button
    private lateinit var btnPause: Button
    private lateinit var btnContinue: Button
    private lateinit var btnReset: Button
    private lateinit var btnEnd: Button
    private lateinit var editTextHours: EditText
    private lateinit var editTextMinutes: EditText
    private lateinit var editTextSeconds: EditText

    private var countDownTimer: CountDownTimer? = null
    private var timeLeftInMillis: Long = 0
    private var isRunning = false
    private var isPaused = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_countdown)

        timerText = findViewById(R.id.timerText)
        btnStart = findViewById(R.id.btnStart)
        btnPause = findViewById(R.id.btnPause)
        btnContinue = findViewById(R.id.btnContinue)
        btnReset = findViewById(R.id.btnReset)
        btnEnd = findViewById(R.id.btnEnd)
        editTextHours = findViewById(R.id.editTextHours)
        editTextMinutes = findViewById(R.id.editTextMinutes)
        editTextSeconds = findViewById(R.id.editTextSeconds)

        btnStart.setOnClickListener { startCountdown() }
        btnPause.setOnClickListener { pauseCountdown() }
        btnContinue.setOnClickListener { continueCountdown() }
        btnReset.setOnClickListener { resetCountdown() }
        btnEnd.setOnClickListener { finish() }

        updateButtonStates()
    }

    private fun startCountdown() {
        val hours = editTextHours.text.toString().toLong() * 60 * 60 * 1000
        val minutes = editTextMinutes.text.toString().toLong() * 60 * 1000
        val seconds = editTextSeconds.text.toString().toLong() * 1000
        timeLeftInMillis = hours + minutes + seconds

        if (!isRunning) {
            isRunning = true
            isPaused = false
            countDownTimer = object : CountDownTimer(timeLeftInMillis, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    timeLeftInMillis = millisUntilFinished
                    updateTimerText()
                }
                override fun onFinish() {
                    timerText.text = "倒计时结束"
                    isRunning = false
                    updateButtonStates()
                }
            }.start()

            updateButtonStates()
        }
    }

    private fun pauseCountdown() {
        if (isRunning) {
            isRunning = false
            isPaused = true
            countDownTimer?.cancel()
            updateButtonStates()
        }
    }

    private fun continueCountdown() {
        if (isPaused) {
            isRunning = true
            isPaused = false
            countDownTimer = object : CountDownTimer(timeLeftInMillis, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    timeLeftInMillis = millisUntilFinished
                    updateTimerText()
                }
                override fun onFinish() {
                    timerText.text = "倒计时结束"
                    isRunning = false
                    updateButtonStates()
                }
            }.start()

            updateButtonStates()
        }
    }

    private fun resetCountdown() {
        isRunning = false
        isPaused = false
        countDownTimer?.cancel()
        timeLeftInMillis = 0
        updateTimerText()
        updateButtonStates()
    }

    private fun updateButtonStates() {
        if (isRunning) {
            // During countdown
            btnStart.isEnabled = false
            btnPause.isEnabled = true
            btnContinue.isEnabled = false
            btnReset.isEnabled = true
        } else if (isPaused) {
            // Countdown paused
            btnStart.isEnabled = false
            btnPause.isEnabled = false
            btnContinue.isEnabled = true
            btnReset.isEnabled = true
        } else {
            // Countdown not started or finished
            btnStart.isEnabled = true
            btnPause.isEnabled = false
            btnContinue.isEnabled = false
            btnReset.isEnabled = true
        }
    }

    private fun updateTimerText() {
        val hours = (timeLeftInMillis / (60 * 60 * 1000)).toInt()
        val minutes = ((timeLeftInMillis % (60 * 60 * 1000)) / (60 * 1000)).toInt()
        val seconds = ((timeLeftInMillis % (60 * 1000)) / 1000).toInt()
        timerText.text = String.format("%02d:%02d:%02d", hours, minutes, seconds)
    }
}
